<template>
  <div>
    <a-modal :open='props.open' :width="props.modelInfo && props.modelInfo.width ? props.modelInfo.width : '900px'"
      :title="props.modelInfo && props.modelInfo.title ? props.modelInfo.title : 'Basic Modal'" @ok='handleOk'
      okText='保存' :confirmLoading='modelConfig.confirmLoading' :destroyOnClose='modelConfig.destroyOnClose'
      @cancel='handleCancel'>
      <template #footer>
        <a-button key='back' @click='handleCancel'>取消</a-button>
        <a-button key='submit' type='primary' :loading='loading' @click='handleOk'>回复</a-button>
      </template>
      <a-form ref='formRef' name='UserFeedbackInfoForm' class='ant-advanced-search-form' :model='formState'
        @finish='onFinish' @finishFailed='onFinishFailed' :rules='rulesRef' :label-col='labelCol'
        :wrapper-col='wrapperCol'>
        <a-row :gutter='24'>
          <a-textarea v-model:value="formState.resolution" placeholder="请填写回复意见" :autoSize="{ minRows: 4 }"
            allow-clear />
        </a-row>
      </a-form>
    </a-modal>
  </div>
</template>
<script lang='ts' setup>
import { UserFeedbackInfoDetail } from './userFeedbackInfoDetailTs';
import {
  getUserFeedbackInfoDetail,
  addOrEditUserFeedbackInfo,
} from '@/api/userInfo/userFeedbackInfo/userFeedbackInfoTs';
import { message, FormInstance } from 'ant-design-vue';
import { ModelInfo } from '../userFeedbackInfoListTs';

const labelCol = ref({ span: 5 });
const wrapperCol = ref({ span: 19 });

let loading = ref<boolean>(false);

const formRef = ref<FormInstance>();

const rulesRef = reactive({
  userId: [
    {
      required: true,
      message: '用户ID不能为空！',
    },
  ],
  description: [
    {
      required: true,
      message: '问题描述不能为空！',
    },
  ],
  status: [
    {
      required: true,
      message: '问题状态: 1=待处理, 2=处理中, 3=已处理不能为空！',
    },
  ],
  handlerId: [
    {
      required: true,
      message: '处理人ID不能为空！',
    },
  ],
  resolution: [
    {
      required: true,
      message: '处理意见不能为空！',
    },
  ],
});

const modelConfig = {
  confirmLoading: true,
  destroyOnClose: true,
};

interface Props {
  open?: boolean;
  modelInfo?: ModelInfo;
}
const props = defineProps<Props>();

let formState = ref<UserFeedbackInfoDetail>({});


const emit = defineEmits(['handleOk', 'handleCancel']);

const handleOk = (): void => {
  loading.value = true;
  if (formRef.value) {
    formRef.value.validateFields().then(
      () => saveUserFeedbackInfoManager()).catch(() => {
        loading.value = false;
      });
  }
};

const handleCancel = (): void => {
  emit('handleCancel', false);
};

//保存用户问题反馈信息表信息
const saveUserFeedbackInfoManager = (): void => {
  let method = '';
  if (formState.value.id) {
    method = 'put';
  } else {
    method = 'post';
  }
  addOrEditUserFeedbackInfo(method, formState.value)
    .then((res) => {
      if (res.code == '200') {
        message.success((res && res.message) || '保存成功！');
        emit('handleOk', false);
      } else {
        message.error((res && res.message) || '保存失败！');
      }
      formState.value = {};
    })
    .catch((error: any) => {
       message.error((error?.message) || '保存失败！');
    }).finally(() => {
      loading.value = false;
    });
}

const onFinish = (values: any): void => {
  console.log('Success:', values);
};

const onFinishFailed = (errorInfo: any): void => {
  console.log('Failed:', errorInfo);
};

const init = (): void => {
  if (props.modelInfo) {
    if (props.modelInfo.id) {
      getUserFeedbackInfoDetail(props.modelInfo.id)
        .then((res) => {
          if (res.code == '200') {
            formState.value = res.data;
            modelConfig.confirmLoading = false;
          } else {
            message.error((res && res.message) || '查询失败！');
          }
        })
        .catch((error: any) => {
          let data = error?.response?.data;
          if (data) {
            message.error((data?.message) || '查询失败！');
          }
        });
    } else {
      modelConfig.confirmLoading = false;
      formState.value = {
      };
    }
  }
}

watch(
  () => props.open,
  (newVal) => {
    if (newVal) {
      init();
    }
  },
  {
    immediate: true,
    deep: true,
  }
);

defineExpose({ handleOk, handleCancel });
</script>
<style lang='scss' scoped></style>